# -*- coding:utf-8 -*-
import sys
sys.path.append("../")
import json
import logging
from flask import Flask
from flask import request
import service.chatbot as chatbot

app = Flask(__name__)
# set logger
LOG_FORMAT = '%(asctime)s %(levelname)s %(message)s'
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO, filename="service.log", filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter(LOG_FORMAT)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
logger = logging.getLogger(__name__)


@app.route('/predict', methods=['POST'])
def predict():
    """make a prediction using the specified model and return the results"""
    data = request.get_json()

    query = data['query']
    ret_size = data['ret_size'] if 'ret_size' in data else 1

    response = chatbot.response(query, ret_size)

    return json.dumps(response)


if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0')
